package com.dems.core.util;

import cn.hutool.core.date.ChineseDate;
import com.dems.core.enumerate.HolidayConstant;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Calendar;
import java.util.Date;

public class HolidayUtil {

    public static Date getHoliday(String holidayName, int year) {
        switch (holidayName) {
            //元旦
            case HolidayConstant.NEW_YEARS_DAY:
                return DateUtil.parse(year + "-1-1");
            //春节
            case HolidayConstant.THE_SPRING_FESTIVAL:
                ChineseDate theSpringFestival = new ChineseDate(year, 1, 1);
                return theSpringFestival.getGregorianDate();
            //清明
            case HolidayConstant.TOMB_SWEEPING_FESTIVAL:
                int param = year - 2000;
                int tombSweepingFestival = (int) (param * 0.2422 + 4.81) - param / 4;
                return DateUtil.parse(year + "-4-" + tombSweepingFestival);
            //劳动节
            case HolidayConstant.LABOUR_DAY:
                return DateUtil.parse(year + "-5-1");
            //端午
            case HolidayConstant.THE_DRAGON_BOAT_FESTIVAL:
                ChineseDate dragonBoatFestival = new ChineseDate(year, 5, 5);
                return dragonBoatFestival.getGregorianDate();
            //中秋
            case HolidayConstant.MID_AUTUMN_FESTIVAL:
                ChineseDate midAutumnFestival = new ChineseDate(year, 8, 15);
                return midAutumnFestival.getGregorianDate();
            //国庆
            case HolidayConstant.NATIONAL_DAY:
                return DateUtil.parse(year + "-10-1");
            default:
                return new Date();
        }
    }

    public static String getHoliday(Date date){
        String[] holidays = {
                "元旦节","春节","清明节","劳动节","端午节","中秋节","国庆节"
        };
        String holiday = null;
        Calendar calendar = Calendar.getInstance();
        for(String hd : holidays){
            Date hdate = getHoliday(hd, calendar.get(Calendar.YEAR));
            if(DateFormatUtils.format(date, "yyyyMMdd").equals(DateFormatUtils.format(hdate, "yyyyMMdd"))){
                holiday = hd;
            }
        }
        return holiday;
    }
}
